<template>
  <div class="panel">
    <div ref="chartRef" class="chart"></div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue'
import * as echarts from 'echarts'

const chartRef = ref<HTMLElement | null>(null)
let chart: echarts.ECharts | null = null

const initChart = () => {
  if (!chartRef.value) return
  
  chart = echarts.init(chartRef.value)
  const option = {
    tooltip: {
      trigger: 'axis',
      formatter: function(params: any[]) {
        const data = params[0];
        return `${data.name}，温度${data.value}℃`;
      },
      axisPointer: {
        type: 'line',
        lineStyle: {
          color: '#33ffd0',
          width: 1,
          type: 'solid'
        }
      }
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      top: '15%',
      containLabel: true
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
      axisLine: {
        lineStyle: {
          color: '#33ffd0'
        }
      },
      axisLabel: {
        color: '#33ffd0'
      }
    },
    yAxis: {
      type: 'value',
      name: '温度(℃)',
      nameTextStyle: {
        color: '#33ffd0'
      },
      splitLine: {
        lineStyle: {
          color: 'rgba(51, 255, 208, 0.1)'
        }
      },
      axisLine: {
        lineStyle: {
          color: '#33ffd0'
        }
      },
      axisLabel: {
        color: '#33ffd0'
      }
    },
    series: [
      {
        type: 'line',
        smooth: true,
        lineStyle: {
          width: 2,
          color: '#33ffd0'
        },
        showSymbol: true,
        areaStyle: {
          opacity: 0.3,
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            {
              offset: 0,
              color: 'rgba(51, 255, 208, 0.5)'
            },
            {
              offset: 1,
              color: 'rgba(51, 255, 208, 0.1)'
            }
          ])
        },
        itemStyle: {
          color: '#33ffd0'
        },
        emphasis: {
          focus: 'series'
        },
        data: [25, 28, 22, 26, 24, 27, 23]
      },
      {
        type: 'line',
        smooth: true,
        symbol: 'none',
        lineStyle: {
          width: 2,
          type: 'dashed',
          color: '#33ffd0'
        },
        data: [20, 20, 20, 20, 20, 20, 20],
        markLine: {
          symbol: ['none', 'none'],
          silent: true,
          data: [
            {
              yAxis: 20,
              label: {
                show: true,
                formatter: '适宜下限 20℃',
                color: '#33ffd0',
                position: 'insideStartTop',
                distance: [220, 5],
                textShadow: '0 0 3px rgba(51, 255, 208, 0.3)',
                padding: [2, 4]
              },
              lineStyle: {
                color: 'transparent'
              }
            }
          ]
        }
      },
      {
        type: 'line',
        smooth: true,
        symbol: 'none',
        lineStyle: {
          width: 2,
          type: 'dashed',
          color: '#F56C6C'
        },
        data: [30, 30, 30, 30, 30, 30, 30],
        markLine: {
          symbol: ['none', 'none'],
          silent: true,
          data: [
            {
              yAxis: 30,
              label: {
                show: true,
                formatter: '适宜上限 30℃',
                color: '#F56C6C',
                position: 'insideStartTop',
                distance: [220, 5],
                textShadow: '0 0 3px rgba(245, 108, 108, 0.3)',
                padding: [2, 4]
              },
              lineStyle: {
                color: 'transparent'
              }
            }
          ]
        }
      }
    ]
  }
  
  chart.setOption(option)
}

// 监听窗口大小变化
const handleResize = () => {
  chart?.resize()
}

onMounted(() => {
  initChart()
  window.addEventListener('resize', handleResize)
})

onUnmounted(() => {
  chart?.dispose()
  window.removeEventListener('resize', handleResize)
})
</script>

<style scoped>
.panel {
  height: 100%;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.chart {
  width: 100%;
  height: 100%;
}
</style> 